package com.qigao.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qigao.mall.api.BillItemService;
import com.qigao.mall.commons.constant.CommonConstants;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.*;
import com.qigao.mall.commons.utils.DateUtil;
import com.qigao.mall.jdbc.mapper.SysDictInfoMapper;
import com.qigao.mall.jdbc.mapper.TbBillItemMapper;
import com.qigao.mall.jdbc.mapper.TbBillOwnerMapper;
import com.qigao.mall.jdbc.mapper.TbItemSkuMapper;
import com.qigao.mall.jdbc.model.SysDictInfo;
import com.qigao.mall.jdbc.model.TbBillItem;
import com.qigao.mall.jdbc.model.TbBillOwner;
import com.qigao.mall.jdbc.model.TbItemSku;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author luiz
 * @Title: com.qigao.mall.service.impl
 * @ProjectName market-parent
 * @date 2020-06-2020-06-18 15:25
 * @Description:
 */
@Service
public class BillItemServiceImpl implements BillItemService {
    @Resource
    private TbBillItemMapper tbBillItemMapper;
    @Resource
    private SysDictInfoMapper sysDictInfoMapper;
    @Resource
    private TbBillOwnerMapper tbBillOwnerMapper;
    @Resource
    private TbItemSkuMapper tbItemSkuMapper;
    @Override
    public Result<String> create(TbBillItemCreateDto tbBillItemCreateDto) {
        /*TbBillItem tbBillItemOld=tbBillItemMapper.selectByItemCode(tbBillItemCreateDto.getItemCode());
        if(tbBillItemOld!=null){
            return Result.newError(ResultCode.COMMON_DATA_EXISTS);
        }*/
        TbBillItem tbBillItem=new TbBillItem();
        BeanUtils.copyProperties(tbBillItemCreateDto,tbBillItem);
        tbBillItem.setCreateNo(tbBillItemCreateDto.getCreateUserNo());
        tbBillItem.setCreateTime(new Date());
        tbBillItem.setModiNo(tbBillItemCreateDto.getCreateUserNo());
        tbBillItem.setModiTime(new Date());
        tbBillItemMapper.insertSelective(tbBillItem);
        return Result.newSuccess();
    }

    @Override
    public Result<String> update(TbBillItemModiDto tbBillItemModiDto) {
        TbBillItem tbBillItem=new TbBillItem();
        BeanUtils.copyProperties(tbBillItemModiDto,tbBillItem);
        tbBillItem.setModiNo(tbBillItemModiDto.getModiUserNo());
        tbBillItem.setModiTime(new Date());
        tbBillItemMapper.updateByPrimaryKeySelective(tbBillItem);
        return Result.newSuccess();
    }

    @Override
    public Result<String> delete(String id) {
        tbBillItemMapper.deleteByPrimaryKey(Long.valueOf(id));
        return Result.newSuccess();
    }

    @Override
    public Page<TbBillItemResultDto> listPage(TbBillItemCondDto tbBillItemCondDto) {
        //使用PageHelper类进行分页
        PageHelper.startPage(tbBillItemCondDto.getPageNum(), tbBillItemCondDto.getPageSize());
        Page<TbBillItem> tbBillItemPage = (Page<TbBillItem>) tbBillItemMapper.selectList(tbBillItemCondDto);
        List<TbBillItem> tbBillItemList = tbBillItemPage.getResult();
        Page<TbBillItemResultDto> tbBillItemResultDtoPage = new Page<>();
        BeanUtils.copyProperties(tbBillItemPage, tbBillItemResultDtoPage);
        List<TbBillItemResultDto> tbBillItemResultDtos = buildDto(tbBillItemList);
        tbBillItemResultDtoPage.addAll(tbBillItemResultDtos);
        return tbBillItemResultDtoPage;
    }

    @Override
    public List<TbBillItemResultDto> listAll(TbBillItemCondDto tbBillItemCondDto) {
        List<TbBillItem> tbBillItemList =  tbBillItemMapper.selectList(tbBillItemCondDto);
        List<TbBillItemResultDto> tbBillItemResultDtos = buildDto(tbBillItemList);
        return tbBillItemResultDtos;
    }

    @Override
    public Result<List<TbBillOwnerResultDto>> findByItemCode(String itemCode) {
        List<SysDictInfo> sysDictInfos=sysDictInfoMapper.findByPDictEnable(CommonConstants.PROFIT_ITEM);
        List<TbBillOwner> tbBillOwners=tbBillOwnerMapper.selectByItemCode(itemCode);
        List<TbBillOwnerResultDto> tbBillOwnerResultDtos=new ArrayList<>();
        for(SysDictInfo sysDictInfo:sysDictInfos){
            TbBillOwnerResultDto tbBillOwnerResultDto=new TbBillOwnerResultDto();
            //tbBillOwnerResultDto.setOwnerType(sysDictInfo.getDictNo());
          //  tbBillOwnerResultDto.setOwnerTypeName(sysDictInfo.getDictName());
            tbBillOwnerResultDto.setChecked(checkExists(tbBillOwnerResultDto,tbBillOwners));
            tbBillOwnerResultDtos.add(tbBillOwnerResultDto);
        }
        return Result.newSuccess(tbBillOwnerResultDtos);
    }

    @Override
    public Result<String> save(TbBillOwnerUpdateDto tbBillOwnerUpdateDto) {
        tbBillItemMapper.deleteByItemCode(tbBillOwnerUpdateDto.getItemCode());
        List<TbBillOwnerDto> tbBillOwnerDtos=tbBillOwnerUpdateDto.getTbBillOwnerDtos();
        if(!CollectionUtils.isEmpty(tbBillOwnerDtos)){
            List<TbBillOwner> tbBillOwners=new ArrayList<>();
            for (TbBillOwnerDto tbBillOwnerDto:tbBillOwnerDtos){
                TbBillOwner tbBillOwner=new TbBillOwner();
                BeanUtils.copyProperties(tbBillOwnerDto,tbBillOwner);
                tbBillOwner.setCreateNo(tbBillOwnerDto.getOperUserNo());
                tbBillOwner.setCreateTime(new Date());
                tbBillOwner.setModiNo(tbBillOwnerDto.getOperUserNo());
                tbBillOwner.setModiTime(new Date());
                tbBillOwners.add(tbBillOwner);
            }
            tbBillOwnerMapper.insetBatch(tbBillOwners);
        }
        return Result.newSuccess();
    }

    @Override
    public Result<List<TbBillOwnerResultDto>> findEnableList() {
        List<TbBillItem> tbBillItems = tbBillItemMapper.selectAll();
        if(CollectionUtils.isEmpty(tbBillItems)){
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
        List<TbBillOwnerResultDto> tbBillOwnerResultDtos=new ArrayList<>();
        for(TbBillItem tbBillItem:tbBillItems){
            TbBillOwnerResultDto tbBillOwnerResultDto=new TbBillOwnerResultDto();
            tbBillOwnerResultDto.setItemCode(tbBillItem.getItemCode());
            tbBillOwnerResultDto.setItemName(tbBillItem.getItemName());
            tbBillOwnerResultDto.setUserLevel(tbBillItem.getUserLevel());
            tbBillOwnerResultDto.setUserType(tbBillItem.getUserType());
            tbBillOwnerResultDto.setProfitPercent(tbBillItem.getProfitPercent());
            tbBillOwnerResultDtos.add(tbBillOwnerResultDto);
        }
        return Result.newSuccess(tbBillOwnerResultDtos);
    }

    @Override
    public Result calBenefitByProd(Integer userType,String userLevel, String prodSaleAmtStr) {
       // TbBillItem tbBillItem=tbBillItemMapper.selectByIndex(UserTypeEnum.OWNER.getCode(),userLevel,BillItemEnum.ONE_INVITE_PROD1_NOTRELEASE.getCode());
        List<SysDictInfo> sysDictInfos=sysDictInfoMapper.selectBypDictNo(DictTypeEnum.RELEASE_PERCENT.getCode());
        Optional<SysDictInfo> sysDictInfoOptional=sysDictInfos.stream().filter(o->o.getDictNo().equals(userType.toString())&&userLevel.equals(o.getDictName())).findFirst();
        if(sysDictInfoOptional.orElse(null)==null)return Result.newSuccess(0);
        SysDictInfo sysDictInfo=sysDictInfoOptional.get();
        BigDecimal profitPercent=new BigDecimal(sysDictInfo.getDictVal());
        BigDecimal prodSaleAmt=new BigDecimal(prodSaleAmtStr);
        //优惠金额
        BigDecimal benefitAmt=prodSaleAmt.multiply(profitPercent).divide(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP);
        return Result.newSuccess(benefitAmt);
    }
/*
    private void fillOwnerResultDto(TbBillOwnerResultDto tbBillOwnerResultDto, List<TbBillItem> tbBillItems) {
        if(CollectionUtils.isEmpty(tbBillItems))return;
        for(TbBillItem tbBillItem:tbBillItems){
            if(tbBillItem.getItemCode().equals(tbBillOwnerResultDto.getItemCode())){
                tbBillOwnerResultDto.setItemName(tbBillItem.getItemName());
                tbBillOwnerResultDto.setItemType(tbBillItem.getItemType());
                tbBillOwnerResultDto.setProfitPercent(tbBillItem.getProfitPercent());
            }
        }
    }*/

    private boolean checkExists(TbBillOwnerResultDto tbBillOwnerResultDto, List<TbBillOwner> tbBillOwners) {
        if(CollectionUtils.isEmpty(tbBillOwners)){
           return false;
        }
        for(TbBillOwner tbBillOwner:tbBillOwners){
            /*if(tbBillOwner.getOwnerType().equals(tbBillOwnerResultDto.getOwnerType())){
                tbBillOwnerResultDto.setItemCode(tbBillOwner.getItemCode());
                tbBillOwnerResultDto.setItemName(tbBillOwner.getItemName());
                tbBillOwnerResultDto.setChecked(true);
                return true;
            }*/
        }
        return false;
    }

    private List<TbBillItemResultDto> buildDto(List<TbBillItem> tbBillItems) {
        if(CollectionUtils.isEmpty(tbBillItems)){
            return null;
        }
        List<TbBillItemResultDto> tbBillItemResultDtos=new ArrayList<TbBillItemResultDto>(tbBillItems.size());
        for(TbBillItem tbBillItem:tbBillItems){
            TbBillItemResultDto tbBillItemResultDto=singleConvertDto(tbBillItem);
            tbBillItemResultDtos.add(tbBillItemResultDto);
        }
        return tbBillItemResultDtos;
    }

    private TbBillItemResultDto singleConvertDto(TbBillItem tbBillItem) {
        TbBillItemResultDto tbBillItemResultDto=new TbBillItemResultDto();
        BeanUtils.copyProperties(tbBillItem,tbBillItemResultDto);
        tbBillItemResultDto.setUserTypeName(UserTypeEnum.getNameByCode(tbBillItem.getUserType()));
        tbBillItemResultDto.setUserLevelName(UserLevelEnum.getNameByCode(tbBillItem.getUserLevel()));
        tbBillItemResultDto.setItemTypeName(ItemTypeEnum.matching(tbBillItem.getItemType()).getName());
        tbBillItemResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbBillItem.getCreateTime()));
        tbBillItemResultDto.setModiTime(DateUtil.getDateTimeFormat(tbBillItem.getModiTime()));
        return tbBillItemResultDto;
    }

}
